package com.byb.server.service.impl;

import com.byb.server.service.OtherSafeguardService;
import com.byb.server.domain.OtherSafeguard;
import com.byb.server.repository.OtherSafeguardRepository;
import com.byb.server.repository.search.OtherSafeguardSearchRepository;
import com.byb.server.service.dto.OtherSafeguardDTO;
import com.byb.server.service.mapper.OtherSafeguardMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing OtherSafeguard.
 */
@Service
@Transactional
public class OtherSafeguardServiceImpl implements OtherSafeguardService{

    private final Logger log = LoggerFactory.getLogger(OtherSafeguardServiceImpl.class);

    private final OtherSafeguardRepository otherSafeguardRepository;

    private final OtherSafeguardMapper otherSafeguardMapper;

    private final OtherSafeguardSearchRepository otherSafeguardSearchRepository;
    public OtherSafeguardServiceImpl(OtherSafeguardRepository otherSafeguardRepository, OtherSafeguardMapper otherSafeguardMapper, OtherSafeguardSearchRepository otherSafeguardSearchRepository) {
        this.otherSafeguardRepository = otherSafeguardRepository;
        this.otherSafeguardMapper = otherSafeguardMapper;
        this.otherSafeguardSearchRepository = otherSafeguardSearchRepository;
    }

    /**
     * Save a otherSafeguard.
     *
     * @param otherSafeguardDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public OtherSafeguardDTO save(OtherSafeguardDTO otherSafeguardDTO) {
        log.debug("Request to save OtherSafeguard : {}", otherSafeguardDTO);
        OtherSafeguard otherSafeguard = otherSafeguardMapper.toEntity(otherSafeguardDTO);
        otherSafeguard = otherSafeguardRepository.save(otherSafeguard);
        OtherSafeguardDTO result = otherSafeguardMapper.toDto(otherSafeguard);
        //otherSafeguardSearchRepository.save(otherSafeguard);
        return result;
    }

    /**
     *  Get all the otherSafeguards.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OtherSafeguardDTO> findAll(Pageable pageable) {
        log.debug("Request to get all OtherSafeguards");
        return otherSafeguardRepository.findAll(pageable)
            .map(otherSafeguardMapper::toDto);
    }

    /**
     *  Get one otherSafeguard by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public OtherSafeguardDTO findOne(Long id) {
        log.debug("Request to get OtherSafeguard : {}", id);
        OtherSafeguard otherSafeguard = otherSafeguardRepository.findOne(id);
        return otherSafeguardMapper.toDto(otherSafeguard);
    }

    /**
     *  Delete the  otherSafeguard by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete OtherSafeguard : {}", id);
        otherSafeguardRepository.delete(id);
        //otherSafeguardSearchRepository.delete(id);
    }

    /**
     * Search for the otherSafeguard corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OtherSafeguardDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of OtherSafeguards for query {}", query);
        Page<OtherSafeguard> result = otherSafeguardSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(otherSafeguardMapper::toDto);
    }
}
